package guowei.guliorder.order.controller;


import com.rabbitmq.client.Channel;
import guowei.common.vo.MemberVo;
import guowei.guliorder.order.entity.OrderEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.Date;

/**
 *  @RabbitListener(queues = {"AdminMq-java-Queue"}) 监听指定队列消息。
 *
 *  @RabbitHandler 根据消息的类型找到当前类下匹配参数类型的方法。
 *
 */
@Slf4j
@Controller
@RabbitListener(queues = {"AdminMq-java-Queue"})
public class ReceiveMessage {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RabbitHandler
    public void ReceiveMemVo(Message message, MemberVo memberVo, Channel channel) {
        log.info("接收到的信息为" + message+"接收到的数据对象为" + memberVo);
        MessageProperties messageProperties = message.getMessageProperties();


//  deliveryTag是channel内按顺序自增的
        long deliveryTag = messageProperties.getDeliveryTag();
        try {
//  签收消息
            channel.basicAck(deliveryTag,false);
            System.out.println("签收消息了");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @RabbitHandler
    public void ReceiveMemVo(Message message, OrderEntity orderEntity, Channel channel) {
        log.info("接收到" + message+"接收到的数据对象为" + orderEntity);
        MessageProperties messageProperties = message.getMessageProperties();
        long deliveryTag = messageProperties.getDeliveryTag();
        try {
//  签收消息
            channel.basicNack(deliveryTag,false,false);
            System.out.println("拒签消息了");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @ResponseBody
    @GetMapping("/sendMsg")
    public String pubMessage(@RequestParam(value = "num", defaultValue = "10") Integer num) {
        for (int i = 0; i < 10; i++) {
            if (i % 2==0){
                MemberVo memberVo = new MemberVo();
                memberVo.setLevelId(1l);
                memberVo.setCity("长江" + i);
                memberVo.setBirth(new Date(102, 9, 0));
                rabbitTemplate.convertAndSend("AdminMq-java-exchange", "ganjiuwanle.com", memberVo);
                log.info("1111");
            }else {
                OrderEntity orderEntity = new OrderEntity();
                orderEntity.setBillContent("这是第"+i+"请求");
                rabbitTemplate.convertAndSend("AdminMq-java-exchange", "ganjiuwanle.com",orderEntity);
                log.info("2222");
            }
        }
        return "ok";
    }
}
